package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import com.ywjh.inter.entity.InsulinPenSysMsgEntity;
import com.ywjh.inter.utils.DateUtil;

public class InsulinPenSysMsgDAO {

	public boolean insert(Connection conn, InsulinPenSysMsgEntity entity) throws SQLException {
		PreparedStatement ps = conn.prepareStatement(
				"insert into insulin_pen_sys_msg(sn,sys_time,a_stu,a,b_stu,b,bat,board_ver,ver,create_time) values(?,?,?,?,?,?,?,?,?,?)");
		ps.setString(1, entity.getSn());
		ps.setString(2, entity.getSysTime());
		ps.setString(3, entity.getaStu());
		ps.setString(4, entity.getA());
		ps.setString(5, entity.getbStu());
		ps.setString(6, entity.getB());
		ps.setString(7, entity.getBat());
		ps.setString(8, entity.getBoardVer());
		ps.setString(9, entity.getVer());
		ps.setTimestamp(10, new Timestamp(new Date().getTime()));

		boolean r = false;
		try {
			r = ps.executeUpdate() > 0;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return r;
	}
	
	/**
	 * 返回指定sn的最新一条数据
	 * @param sn
	 * @return
	 */
	public InsulinPenSysMsgEntity getNewBySn(Connection conn, String sn) throws SQLException {
		PreparedStatement ps = conn.prepareStatement("select * from insulin_pen_sys_msg where sn=? order by create_time desc limit 1");
		ps.setString(1, sn);
		ResultSet rs = ps.executeQuery();
		if(rs.next()) {
			InsulinPenSysMsgEntity entity = new InsulinPenSysMsgEntity();
			entity.setSn(rs.getString("sn"));
			entity.setSysTime(DateUtil.parseStr(new Date(Long.parseLong(rs.getString("sys_time"))), "yyyy-MM-dd HH:mm:ss"));
			entity.setaStu(rs.getString("a_stu"));
			entity.setA(rs.getString("a"));
			entity.setbStu(rs.getString("b_stu"));
			entity.setB(rs.getString("b"));
			entity.setBat(rs.getString("bat"));
			entity.setBoardVer(rs.getString("board_ver"));
			entity.setVer(rs.getString("ver"));
			return entity;
		}
		return null;
	}
}
